package com.freewater.springboot.demo.aop.api.targetsource.pooling;

import com.freewater.springboot.demo.PackageUtils;
import org.springframework.aop.target.PoolingConfig;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        GenericApplicationContext applicationContext = new GenericApplicationContext();
        final String xmlPath = PackageUtils.getDefaultAppContextXmlPath(Main.class);
        new XmlBeanDefinitionReader(applicationContext).loadBeanDefinitions(xmlPath);
        applicationContext.refresh();
        applicationContext.start();
        applicationContext.registerShutdownHook(); // 注册jvm关闭回调，jvm关闭时一并关闭spring容器

        Object bean = applicationContext.getBean("businessService");
        BusinessService businessService = (BusinessService) bean;
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(businessService::doSomething);
            threadList.add(thread);
        }
        threadList.forEach(Thread::start);
        threadList.forEach(thread -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        PoolingConfig poolingConfig = (PoolingConfig) businessService;
        System.out.println(poolingConfig.getActiveCount());
        System.out.println(poolingConfig.getIdleCount());
        System.out.println(poolingConfig.getMaxSize());
    }
}
